import { Component, OnInit } from '@angular/core';
import { List } from 'linqts';
import { QuotaRank } from "../../../entity/quota-rank.model";
import { DatePipe } from "@angular/common";
import { QuotaRankService } from "../../../service/quota-rank.service";
import { ApplicationService } from '../../../service/application.service';

@Component({
	selector: '',
	templateUrl: './quota-rank.component.html',
	styleUrls: ['./quota-rank.component.css']
})
export class QuotaRankComponent implements OnInit {
	/**
	 * 时间当前选中下标
	 *
	 * @type {number}@memberof PerformanceListComponent
	 */
	public active: number = 0;

	/**
	 * 损失电量当前选中下标(0,1,2,3,4)
	 *
	 * @type {number}@memberof PerformanceListComponent
	 */
	public quotaType: number = 0;

	// 时间类型(年、季度、月)
	private dateType: number;

	// 风场id
	private farmId: number;

	// tab切换对应损失电量
	public quotaTypeStr: string = "发电量";
	public quotaTypeUtil: string = "发电量: 万kWh";
	public quotaTypeUtil2: string = "发电量: 万kWh";

	//时间选择
	public years: string[] = ["2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010"];
	public currentYear: string = new Date().getFullYear().toString();
	public months: string[] = ["12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"];
	public currentMonth: string = (new Date().getMonth() + 1).toString();
	public day: Date = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1);
	public currentDay: string = this.datePipe.transform(this.day, "yyyy-MM-dd");

	//排名1,2标识;
	public ranks: string[] = ['前', '后']
	public currentRank: string = this.ranks[0];
	private rankType: number = 1;
	public rankValue: number = 20;

	/**
   * 倍率
   *
   * @public
   * @type {number}@memberof MainComponent
   */
	public ratio: number = 1;


	/**
	 * echart series(年)
	 *
	 * @type {any[]}@memberof TurbineTrendComponent
	 */
	yearFarmSeries = [];
	yearFarmXAxis: string[] = [];

	yearTurbSeries = [];
	yearTurbXAxis: string[] = [];

	/**
	 * echart series(日)
	 *
	 * @type {any[]}@memberof TurbineTrendComponent
	 */
	dayFarmSeries = [];
	dayFarmXAxis: string[] = [];

	dayTurbSeries = [];
	dayTurbXAxis: string[] = [];

	/**
	 * echart series(月度)
	 *
	 * @type {any[]}@memberof TurbineTrendComponent
	 */
	monthFarmSeries = [];
	monthFarmXAxis: string[] = [];

	monthTurbSeries = [];
	monthTurbXAxis: string[] = [];
	/**
	 *日期格式化
	 *
	 * @type {*}
	 * @memberof QuotaRankComponent
	 */
	en: any;
	constructor(
		private quotaRankService: QuotaRankService,
		private datePipe: DatePipe,
		private application: ApplicationService,

	) { }

	ngOnInit() {
		this.en = {
			firstDayOfWeek: 0,
			dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
			dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
			dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
			monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
			monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
			today: '今天',
			clear: '清除'
		};
		if (this.application.currentApplicationParams && this.application.currentApplicationParams['ratio']) {
			this.ratio = Number(this.application.currentApplicationParams['ratio'].paramValue);
		  }
		this.onSearch();
	}

	public onClickHeader(n: number) {
		this.active = n;
		this.onSearch();
	}
	public onChangeYear(year: string) {
		this.currentYear = year;
	}

	public onChangeMonth(month: string) {
		this.currentMonth = month;
	}
	public onChangeDay(day: string) {
		this.currentDay = this.datePipe.transform(day, "yyyy-MM-dd");
	}
	public onChangeRank(event) {
		this.currentRank = event;
		if (this.currentRank == '前') {
			this.rankType = 1;
		} else {
			this.rankType = 2;
		}
	}

	public onClickQuota(n: number) {

		this.quotaType = n;
		if (this.quotaType == 0) {
			this.quotaTypeStr = "发电量";
			this.quotaTypeUtil = "发电量: 万kWh";
			this.quotaTypeUtil2 = "发电量: 万kWh";
		} else if (this.quotaType == 1) {
			this.quotaTypeStr = "损失电量";
			this.quotaTypeUtil = "损失电量: 万kWh";
			this.quotaTypeUtil2 = "损失电量: 万kWh";
		} else if (this.quotaType == 2) {
			this.quotaTypeStr = "风速";
			this.quotaTypeUtil = "风速: m/s";
			this.quotaTypeUtil2 = "风速: m/s";
		} else if (this.quotaType == 3) {
			this.quotaTypeStr = "可利用率";
			this.quotaTypeUtil = "可利用率: %";
			this.quotaTypeUtil2 = "可利用率: %";
		} else {
			this.quotaTypeStr = "可利用小时数";
			this.quotaTypeUtil = "可利用小时数: 小时";
			this.quotaTypeUtil2 = "可利用小时数: 小时";
		}
		this.onSearch();
	}

	public onFarmColumnClick(event) {
		this.farmId = event;
		this.getQuotaListByFarm(this.active);
	}

	public onSearch() {
		this.yearFarmSeries = [];
		this.yearFarmXAxis = [];
		this.yearTurbSeries = [];
		this.yearTurbXAxis = [];
		this.monthFarmSeries = [];
		this.monthFarmXAxis = [];
		this.monthTurbSeries = [];
		this.monthTurbXAxis = [];
		this.dayFarmSeries = [];
		this.dayFarmXAxis = [];
		this.dayTurbSeries = [];
		this.dayTurbXAxis = [];

		this.initEchartsData();
	}

	private initEchartsData() {
		let dateTime: Date; let dateType: number;
		if (this.active == 0) {
			dateType = 3;
			dateTime = new Date(this.currentYear);
			this.getData(dateType, dateTime);
		} else if (this.active == 1) {
			dateType = 2;
			dateTime = new Date(this.currentYear + "-" + this.currentMonth);
			this.getData(dateType, dateTime);
		} else {
			dateType = 1;
			dateTime = this.day;
			this.getData(dateType, dateTime);
		}

	}

	private getData(dateType, dateTime) {
		let originalFarmQuota: QuotaRank[] = [];
		let quotaFarmData: QuotaRank[] = [];
		let originalTurbQuota: QuotaRank[] = [];
		let quotaTurbData: QuotaRank[] = [];

		this.quotaRankService.getFarmQuotaRankList(this.farmId, this.quotaType, dateType, dateTime, this.rankType, this.rankValue)
			.then(res => {
				// console.log(res)
				if (!res) return false;
				originalFarmQuota = [...res.rankingDataFarmList];
				quotaFarmData = originalFarmQuota;

				originalTurbQuota = [...res.rankingDataTurbineList];
				quotaTurbData = originalTurbQuota;
				// console.log(quotaTurbData)
				let seriesObj: SeriesObj;

				if (quotaFarmData != null && quotaTurbData != null) {
					seriesObj = this.getColumnSeries(quotaFarmData, quotaTurbData);
					// farmSeries.push(...seriesObj.farmSeries);
					// farmXAxis.push(...seriesObj.farmXAxis);
					// turbSeries.push(...seriesObj.turbSeries);
					// turbXAxis.push(...seriesObj.turbXAxis.map(function (str) {
					// 	return str.replace(' ', '\n');
					// }));
					if (dateType == 3) {
						this.yearFarmSeries = seriesObj.farmSeries;
						this.yearFarmXAxis = seriesObj.farmXAxis;
						this.yearTurbSeries = seriesObj.turbSeries;
						this.yearTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
						// console.log(this.yearTurbSeries)

					} else if (dateType == 2) {
						this.monthFarmSeries = seriesObj.farmSeries;
						this.monthFarmXAxis = seriesObj.farmXAxis;
						this.monthTurbSeries = seriesObj.turbSeries;
						this.monthTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					} else {
						this.dayFarmSeries = seriesObj.farmSeries;
						this.dayFarmXAxis = seriesObj.farmXAxis;
						this.dayTurbSeries = seriesObj.turbSeries;
						this.dayTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					}
				}

			})
			.catch(error => {
				console.log("app-quota-rank has error", error);
			});
	}

	private getQuotaListByFarm(n) {
		let dateTime: Date;
		let dateType: number;
		if (n == 0) {
			dateType = 3;
			dateTime = new Date(this.currentYear);
			this.getOneFarmData(dateType, dateTime);
		} else if (n == 1) {
			dateType = 2;
			dateTime = new Date(this.currentYear + "-" + this.currentMonth);
			this.getOneFarmData(dateType, dateTime);
		} else {
			dateType = 1;
			dateTime = this.day;
			this.getOneFarmData(dateType, dateTime);
		}

	}

	private getOneFarmData(dateType, dateTime) {
		let originalTurbQuota: QuotaRank[] = [];
		let quotaTurbData: QuotaRank[] = [];

		this.quotaRankService.getTurbQuotaRankListByFarmId(this.farmId, this.quotaType, dateType, dateTime, this.rankType, this.rankValue)
			.then(res => {
				if (res) {
					originalTurbQuota = [...res];
				};
				quotaTurbData = originalTurbQuota;
				let seriesObj: SeriesObj;
				if (quotaTurbData != null) {
					seriesObj = this.getFarmTurbColumnSeries(quotaTurbData);
					if (dateType == 3) {
						this.yearTurbSeries = seriesObj.turbSeries;
						this.yearTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					} else if (dateType == 2) {
						this.monthTurbSeries = seriesObj.turbSeries;
						this.monthTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					} else {
						this.dayTurbSeries = seriesObj.turbSeries;
						this.dayTurbXAxis = seriesObj.turbXAxis.map(function (str) {
							return str.replace(' ', '\n');
						})
					}
				}
			})
			.catch(error => {
				console.log("app-quota-rank has error", error);
			});
	}
	private getColumnSeries(farmData: QuotaRank[], turbData: QuotaRank[]) {
		const farmSeries = [];
		const farmXAxis = [];
		const turbSeries = [];
		const turbXAxis = [];
		let farmList: QuotaRank[] = farmData;
		let turbineList: QuotaRank[] = turbData;

		let seriesDataF: FarmSeriesId[] = [];
		let seriesDataT: number[] = [];
		// console.log(turbData)
		switch (this.quotaType) {
			case 0:
				farmList.forEach(o => {
					seriesDataF.push({ value: Number(Number(o.value >= 0 ? o.value * this.ratio : 0 * this.ratio).toFixed(2)), id: o.farmId });
					farmXAxis.push(o.farmName);
				});

				farmSeries.push({
					name: '发电量',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}

						},
					},
					data: seriesDataF
				})

				turbineList.forEach(o => {
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
					seriesDataT.push(Number(Number(o.value >= 0 ? o.value * this.ratio : 0 * this.ratio).toFixed(2)));
				})

				turbSeries.push({
					name: '发电量',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						}
					},
					data: seriesDataT,
				})
				break;
			case 1:
				farmList.forEach(o => {
					seriesDataF.push({ value: Number(Number(o.value >= 0 ? o.value * this.ratio : 0 * this.ratio).toFixed(2)), id: o.farmId })
					farmXAxis.push(o.farmName);
				})

				farmSeries.push({
					name: '损失电量',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(Number(Number(o.value >= 0 ? o.value * this.ratio : 0 * this.ratio).toFixed(2)))
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '损失电量',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 2:
				farmList.forEach(o => {
					seriesDataF.push({ value: o.value, id: o.farmId })
					farmXAxis.push(o.farmName);
				})
				farmSeries.push({
					name: '风速',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(o.value)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				// console.log(turbineList)
				turbSeries.push({
					name: '风速',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 3:
				farmList.forEach(o => {
					seriesDataF.push({ value: o.value, id: o.farmId })
					farmXAxis.push(o.farmName);
				})

				farmSeries.push({
					name: '可利用率',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataF
				})

				turbineList.forEach(o => {
					seriesDataT.push(o.value)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '可利用率',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 4:
				let farmDataOrder;
				let turbineListOrder;
				if (this.rankType == 2) {
					farmDataOrder = new List(farmList).OrderBy(o => Number(o.value)).ToArray();
					turbineListOrder = new List(turbineList).OrderBy(o => Number(o.value)).ToArray();
				} else {
					farmDataOrder = new List(farmList).OrderBy(o => Number(o.value)).ToArray().reverse();
					turbineListOrder = new List(turbineList).OrderBy(o => Number(o.value)).ToArray().reverse();
				}

				farmDataOrder.forEach(o => {
					seriesDataF.push({ value: o.value, id: o.farmId })
					farmXAxis.push(o.farmName);
				})
				farmSeries.push({
					name: '可利用小时数',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataF
				})

				turbineListOrder.forEach(o => {
					seriesDataT.push(o.value)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})

				turbSeries.push({
					name: '可利用小时数',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
		}

		let columnDatas: SeriesObj = {
			farmSeries: farmSeries,
			farmXAxis: farmXAxis,
			turbSeries: turbSeries,
			turbXAxis: turbXAxis,
		}
		// console.log(turbXAxis)
		return columnDatas;
	}

	private getFarmTurbColumnSeries(turbData: QuotaRank[]) {

		let turbSeries = [];
		let turbXAxis = [];

		let turbineList: QuotaRank[] = turbData;

		let seriesDataF: FarmSeriesId[] = [];
		let seriesDataT: number[] = [];

		switch (this.quotaType) {
			case 0:
				turbineList.forEach(o => {
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
					seriesDataT.push(Number(Number(o.value >= 0 ? o.value * this.ratio : 0 * this.ratio).toFixed(2)));
				})
				turbSeries.push({
					name: '发电量',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 1:
				turbineList.forEach(o => {
					seriesDataT.push(Number(Number(o.value >= 0 ? o.value * this.ratio : 0 * this.ratio).toFixed(2)))
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '损失电量',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 2:
				turbineList.forEach(o => {
					seriesDataT.push(o.value >= 0 ? o.value : 0)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '风速',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 3:
				turbineList.forEach(o => {
					seriesDataT.push(o.value)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})
				turbSeries.push({
					name: '可利用率',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
			case 4:
				let turbineListOrder;
				if (this.rankType == 2) {
					turbineListOrder = new List(turbineList).OrderBy(o => Number(o.value)).ToArray();
				} else {
					turbineListOrder = new List(turbineList).OrderBy(o => Number(o.value)).ToArray().reverse();
				}
				turbineListOrder.forEach(o => {
					seriesDataT.push(o.value)
					turbXAxis.push(o.farmName + ' ' + o.deviceName)
				})

				turbSeries.push({
					name: '可利用小时数',
					type: 'bar',
					label: {
						normal: {
							show: true,
							position: 'top',
							textStyle: {
								color: '#43a0b3'
							}
						},

					},
					data: seriesDataT
				})
				break;
		}

		let columnDatas: SeriesObj = {
			farmSeries: [],
			farmXAxis: [],
			turbSeries: turbSeries,
			turbXAxis: turbXAxis,
		}

		return columnDatas;
	}
}

interface SeriesObj {
	farmSeries: any[],
	farmXAxis: any[],
	turbSeries: any[],
	turbXAxis: any[],
}

interface FarmSeriesId {
	value: number;
	id: number;
}



// WEBPACK FOOTER //
// ./src/app/module/data-analysis/quota-rank/quota-rank.component.ts
